<jsp:useBean id="rr" class="bibbletex.models.RequestReference"
	scope="page" />
<jsp:useBean id="sessiondata" class="bibbletex.models.SessionData"
	scope="session" />
<jsp:setProperty name="rr" property="*" />
<%@ page import="java.util.ArrayList,bibbletex.models.Collaboration, bibbletex.models.Reference"
         import="bibbletex.models.SessionData,bibbletex.helpers.BibtexFormat, java.io.*" 
         import="bibbletex.helpers.FileHelper, java.util.List, java.util.UUID"%>

<%
	Collaboration c = sessiondata.collaboration;
	Reference r;
	List<Reference> refs = new ArrayList<Reference>();
	boolean refererimport = request.getHeader("referer").contains("import.jsp");
	
	if (c != null){
		if (refererimport){
			if (rr.getBibtexcode() != null)
				refs.addAll(BibtexFormat.toReference(rr.getBibtexcode().replace("\n", "").replace("\r", "")));
			else {
				String contentType = request.getContentType();
			    if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
					String str = FileHelper.readFile(new DataInputStream(request.getInputStream()), request.getContentLength(), request.getContentType());
					refs.addAll(BibtexFormat.toReference(str.replace("\n", "").replace("\r", "")));				
				}
			}
			for (Reference ref : refs)
			{
				ref.setIs_suggestion(!sessiondata.collaboration_is_editable);
				c.addReference(ref);
			}
		}
		else
		{
			String rid_parameter = request.getParameter("referenceID");
			if (rid_parameter != null && rid_parameter.length() > 2)
				r = new Reference(rr, UUID.fromString(rid_parameter));
			else
				r = new Reference(rr);
			r.setIs_suggestion(!sessiondata.collaboration_is_editable);
			c.addReference(r);
			sessiondata.reference = r;
		}
	}
%>

<% if (c != null) { %>
	<jsp:forward page="/collaboration/show.jsp">
		<jsp:param value="<%=(refs.size() == 0 && refererimport)?"No references could be imported": "" %>" name="message" />
	</jsp:forward>
<% } else { %>

<%
	String page_title = new String("Process reference");
	boolean show_title = true;
	String onloadjsfunction = "";
	String[] jstitle={"collab"};
	boolean show_navigation = true;
%>
<%@ include file="/header.jsp"%>

<div class="error">Unable to load Collaboration.
<br />Please Contact our Support.</div>

<%@ include file="/footer.jsp"%>

<% } %>
